import { _decorator } from "cc";
import { Ab_DataTransfer_bA } from "../A-LIB/lib.b.data";
import { Ab_UI_bA } from "./ui.origin";

const {ccclass, property} = _decorator;

@ccclass
export abstract class Ab_BaseUI_bA extends Ab_UI_bA{
    protected Ab___listen_list___bA: ReturnType<typeof this.Ab_listening_bA> = [];

    /** 注册监听 */
    protected Ab_listening_bA(): ReturnType<typeof Ab_DataTransfer_bA.Ab_follow_bA>[] {return []};
    /** 取消监听 */
    protected Ab_closeListen_bA(){
        this.Ab___listen_list___bA.forEach(reg=>Ab_DataTransfer_bA.Ab_cancel_bA(reg));
		abi.Ab_array_bA.Ab_clear_bA(this.Ab___listen_list___bA);
    };

    protected onLoad(): void {
		this.Ab___ui_loaded_bA = false;
        super.onLoad();
        this.Ab___listen_list___bA = this.Ab_listening_bA();
        this.Ab___ui_loaded_bA = true;
        this.Ab_call_cache_bA.forEach(el=>{
            let [func, param, np] = el;
			np.Ab_resolve_bA(Reflect.apply(func, this, param));
        });
		abi.Ab_array_bA.Ab_clear_bA(this.Ab_call_cache_bA);
    }

    protected onDestroy(){
        this.Ab_closeListen_bA();
    }

	private Ab___ui_loaded_bA: boolean;
	/** ui加载状态 */
    protected get Ab_ui_loaded_bA(){
		return this.Ab___ui_loaded_bA;
	};
    private Ab_call_cache_bA: [Function, any[], abi.Ab_NudityPromise_bA<any>][] = [];
    protected Ab_call_func_bA<F extends (...param: any)=>any, R extends ReturnType<F>>(func: F, ...param: Parameters<F>): R|Promise<R>{
        if(this.Ab_ui_loaded_bA) return Reflect.apply(func, this, param);
        else{
			let np = new abi.Ab_NudityPromise_bA<R>();
			this.Ab_call_cache_bA.push([func, param, np]);
			return np.Ab_promise_bA;
		}
    }

	private Ab___late_call_list___bA: Array<[Function, any[], Function]> = [];
	protected lateUpdate(dt: number): void {
		if(this.Ab___late_call_list___bA.length>0){
			let [call, args, complete] = this.Ab___late_call_list___bA.pop();
			typeof complete=='function' ? complete(call(...args), args) : call(...args);
		}
	}

	/**
	 * 在帧后执行回调
	 * @param call
	 * @param args
	 * @param complete
	 */
	protected Ab_frameLateCall_bA<C extends (...args: any[])=>any, Param extends Parameters<C>, R extends ReturnType<C>>(call: C, args: Param, complete?: (r: R, args: Param)=>void){
		this.Ab___late_call_list___bA.unshift([call, args, complete]);
	}

	/**
	 * 帧循环
	 * @param list
	 * @param call
	 * @returns
	 */
	protected Ab_frameWhile_bA<T extends any[]>(list: T, call: (el: T[number], index: number, arr: T)=>void){
		return new Promise<0>(s=>list.forEach((e, i, a)=>this.Ab_frameLateCall_bA(call, [e, i, list], (_, [e, i, a])=>i==list.length-1 ? s(0) : void 0)));
	}
}